
CX = g++
CX_FLAGS = -g -Wall -std=c++11

########################################################################

CFG_FILE = echoer.conf
OUT_FILE = echoer
OUT_FILE_GPERF = echoer.gperf

OBJ_ROOT = ./obj
SRC_DIRS = ./
INC_DIRS = ../../

SRC_LIST = $(foreach sub,$(SRC_DIRS),$(wildcard $(sub)/*.cpp $(sub)/*.cc))
DEP_LIST = $(foreach src,$(SRC_LIST),$(basename $(notdir $(src))).d)
OBJ_LIST = $(foreach src,$(SRC_LIST),$(basename $(notdir $(src))).o)

vpath %.d $(OBJ_ROOT)
vpath %.o $(OBJ_ROOT)
vpath %.cpp $(foreach sub,$(SRC_DIRS),$(wildcard $(sub)) :)
vpath %.cc $(foreach sub,$(SRC_DIRS),$(wildcard $(sub)) :)

########################################################################

.PHONY : all 

all : $(OUT_FILE) $(OUT_FILE_GPERF)

$(OUT_FILE) : mkdir $(DEP_LIST) $(OBJ_LIST)
	$(CX) ${CX_FLAGS} ${foreach obj,$(OBJ_LIST),$(OBJ_ROOT)/$(obj)} -lssl -lcrypto -lrabbitmq -ltcmalloc -o $@

$(OUT_FILE_GPERF) : mkdir $(DEP_LIST) $(OBJ_LIST)
	$(CX) ${CX_FLAGS} ${foreach obj,$(OBJ_LIST),$(OBJ_ROOT)/$(obj)} -lssl -lcrypto -lrabbitmq -lprofiler -o $@

%.d : %.cpp
	$(CX) ${CX_FLAGS} ${foreach sub,${INC_DIRS},-I${sub}} -MM $< > $(OBJ_ROOT)/$@

%.d : %.cc
	$(CX) ${CX_FLAGS} ${foreach sub,${INC_DIRS},-I${sub}} -MM $< > $(OBJ_ROOT)/$@

%.o : %.cpp
	$(CX) ${CX_FLAGS} ${foreach sub,${INC_DIRS},-I${sub}} -c $< -o $(OBJ_ROOT)/$@

%.o : %.cc
	$(CX) ${CX_FLAGS} ${foreach sub,${INC_DIRS},-I${sub}} -c $< -o $(OBJ_ROOT)/$@

########################################################################

.PHONY : mkdir
mkdir :
	-mkdir -p $(OBJ_ROOT)

.PHONY : install
install :
	-cp -f $(CFG_FILE) /usr/local/etc/
	-cp -f $(OUT_FILE) /usr/local/bin/
	-cp -f $(OUT_FILE_GPERF) /usr/local/bin/
	
.PHONY : clean
clean :
	rm -rf $(OBJ_ROOT)/*.d $(OBJ_ROOT)/*.o $(OUT_FILE) $(OUT_FILE_GPERF)

.PHONY : debug
debug :
	@echo $(ROOT_DIR)
	@echo $(SRC_LIST)
	@echo $(INC_LIST)
	@echo $(OBJ_LIST)
	@echo $(DEP_LIST)
	@echo $(VPATH)

########################################################################

-include ${foreach dep,$(DEP_LIST),$(OBJ_ROOT)/$(dep)}
